package com.zeros.mysplashtwo.view.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.zeros.mysplashtwo.MainActivity;
import com.zeros.mysplashtwo.MyApplication;
import com.zeros.mysplashtwo.R;
import com.zeros.mysplashtwo.base.BaseActivity;
import com.zeros.mysplashtwo.utils.Constants;
import com.zeros.mysplashtwo.utils.SPUtilsTwo;
import com.zeros.mysplashtwo.utils.ToastUtilTwo;
import com.zeros.mysplashtwo.utils.ZlogUtilTwo;
import com.zeros.mysplashtwo.view.weidge.ProgressWebView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 承载广告的界面
 */
public class WebViewActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.wv)
    ProgressWebView webView;

    private long djtime[] = new long[2];
    private String from;
    String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        ButterKnife.bind(this);

        setSupportActionBar(toolbar);
        iniBase();

        String title = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        from = getIntent().getStringExtra("from");
        toolbar.setTitle(title);

        setWebView();
    }

    private void setWebView() {
        setWebSetting();

        webView.setWebViewClient(new WebViewClient() {
            //防止调用系统的浏览器
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

//        webView.setWebChromeClient(new WebChromeClient(){
//            @Override
//            public void onProgressChanged(WebView view, int newProgress) {
//                ZlogUtilTwo.e("==progress==>"+String.valueOf(newProgress));
//            }
//
//            @Override
//            public void onReceivedTitle(WebView view, String title) {
//                super.onReceivedTitle(view, title);
//                ZlogUtilTwo.e("==Title==>"+title);
//            }
////            有这个好像就能通过webview定位了
//            @Override
//            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
//                callback.invoke(origin, true, false);
//                super.onGeolocationPermissionsShowPrompt(origin, callback);
//            }
//        });

    }

    @SuppressLint("SetJavaScriptEnabled")
    private void setWebSetting() {
        WebSettings webSettings = webView.getSettings();
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setJavaScriptEnabled(true);
        //设置背景颜色 透明
//        webView.setBackgroundColor(Color.argb(0, 0, 0, 0));
        webSettings.setSupportZoom(true);

        //启用数据库
//        webSettings.setDatabaseEnabled(true);
////        设置定位的数据库路径
//        String dir = this.getApplicationContext()
//                .getDir("database", Context.MODE_PRIVATE)
//                .getPath();
//        webSettings.setGeolocationDatabasePath(dir);
        //启用地理定位
        webSettings.setGeolocationEnabled(true);

        //开启DomStorage缓存
        webSettings.setDomStorageEnabled(true);

        webView.loadUrl(url);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //判断去登录界面还是主界面
        if (from != null && from.equals("advertising")) {
            Intent intent = null;
            String token = (String) SPUtilsTwo.get(this, Constants.TOKEN_KEY, "");
            if (TextUtils.isEmpty(token)) {
                intent = new Intent(this, LoginActivity.class);
            } else {
                intent = new Intent(this, MainActivity.class);
                MyApplication.setToken(token);
            }
            startActivity(intent);
            finish();
        }

    }

    //此方法兼容Android 1.0到Android 2.1 也是常规方法
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断是否来自广告页面
//        if (from != null && from.equals("advertising")) {
//            onBackPressed();
//            return true;
//        }
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        } else {
            System.arraycopy(djtime, 1, djtime, 0, djtime.length - 1);
            djtime[djtime.length - 1] = SystemClock.uptimeMillis();
            if (djtime[0] >= (SystemClock.uptimeMillis() - 1000)) {
//                进入主界面
                onBackPressed();
                finish();
            } else {
                ToastUtilTwo.showShort(this, "再按一次进入主界面");
            }
            return true;
        }
    }

}
